 #!/bin/bash
SCRIPT_PATH=$(cd "$(dirname "$0")" || exit; pwd)
PRODUCT_PATH=$(cd ${SCRIPT_PATH}/..;pwd)


## 判断状态码输出日志，异常不退出
echo_cs(){
    if [[ "$?" -eq 0 ]];then
        echo -e "\e[1;32m$(date +'%F %T') [INFO] - $* Success! \e[0m"
    else
        echo -e "\e[1;31m$(date +'%F %T') [FAIL] - $* Fail! \e[0m"
    fi
}


## 检查目录或文件是否存在
function check_dir_file(){
    if [ ! -d "$1" ] && [ ! -f "$1" ] ;then
        echo_cs "Directory or file $1 does not exist, please check!"
        exit 1
    fi
}


 ## 获取需要操作的product list
function get_product_list(){
    local product_conf=${SCRIPT_PATH}/product-list.conf
    check_dir_file "$product_conf"
    PRODUCT_LIST=""
    while read -r product || [[ -n ${product} ]]
    do
        if [[ ! $product =~ ^#.*$ ]] && [[ ! $product =~ ^$ ]] ;then
            PRODUCT_LIST="${PRODUCT_LIST} ${product}"
        fi
    done < "${product_conf}"
}


function git_clone_product(){
    for product in ${PRODUCT_LIST}
    do
        if [ -d "${PRODUCT_PATH}/${product}" ];then
            error >> /dev/null 2>&1
            echo_cs "${product} directory is exist."
        else
            cd "${PRODUCT_PATH}" || exit 1
            git clone git@gitlab.fano.ai:fanolabs/infra/fano-umbrella-charts/"${product}".git
            echo_cs "git clone git@gitlab.fano.ai:fanolabs/infra/fano-umbrella-charts/${product}.git"
        fi
    done
}


function git_pull_product(){
    for product in ${PRODUCT_LIST}
    do
        cd "${PRODUCT_PATH}/${product}" || exit 1
        git pull
        echo_cs "git pull git@gitlab.fano.ai:fanolabs/infra/fano-umbrella-charts/${product}.git"]
    done
}


function remove_gitfile(){
    for product in ${PRODUCT_LIST}
    do
        cd "${PRODUCT_PATH}/${product}" && rm -rf .git
        echo_cs "rm -rf ${PRODUCT_PATH}/${product}/.git"
    done
    cd "${SCRIPT_PATH}" && rm -rf .git
    echo_cs "rm -rf ${SCRIPT_PATH}/.git"
}


function git_checkout(){
    local BRANCH_NAME=$1
    for product in ${PRODUCT_LIST}
    do
        cd "${PRODUCT_PATH}/${product}"
        git checkout ${BRANCH_NAME}
        echo_cs "git checkout ${BRANCH_NAME}"
    done
}


function create_tag(){
    TODAY=`date +%Y-%m-%d`
    local TAG_NAME=$1
    echo "TAG_NAME is ${TAG_NAME}."
    for product in ${PRODUCT_LIST}
    do
        cd "${PRODUCT_PATH}/${product}"
        git config user.email "jenkins@fano.ai"
        git config user.name "jenkins"
        if [[ ! -n ${TAG_NAME} ]];then
            echo "TAG_NAME is null, exit now"
            return 1
        else
            # 获取是否已有tag
            n=$(git tag -l | grep -cE "^${TAG_NAME}$")
            if [[ ${n} == 1 ]];then
                echo "${TAG_NAME}  already exist,delete ${TAG_NAME} tag"
                git tag -d ${TAG_NAME} && git push origin --delete ${TAG_NAME}
            fi
            #创建tag并push到git仓库
            git tag  ${TAG_NAME}  -m "${TODAY}" && git push origin ${TAG_NAME}
            if [[ $? == 0 ]]; then
                echo "Succeed to create tag."
            else
                echo "Failed to create tag."
                return 1
            fi
        fi
        cd ..
    done
}

# 创建分支
function create_branch(){
    TODAY=`date +%Y-%m-%d`
    local BRANCH_NAME=${1}
    echo "BRANCH_NAME is ${BRANCH_NAME}."
    for product in ${PRODUCT_LIST}
    do
        cd "${PRODUCT_PATH}/${product}"
        git config user.email "jenkins@fano.ai"
        git config user.name "jenkins"
        if [[ ! -n ${BRANCH_NAME} ]];then
            echo "BRANCH_NAME is null, exit now"
            exit 1
        else
            # 获取是否已有branch，如有异常退出
            n=$(git branch -r | grep -cE "origin/${BRANCH_NAME}$")
            if [[ ${n} == 1 ]];then
                echo "${BRANCH_NAME} already exist,please check !!!"
                exit 1
            fi
            #基于master创建branch并push到git仓库
            git checkout master
            git checkout -b ${BRANCH_NAME}  && git push origin ${BRANCH_NAME}
            if [[ $? == 0 ]]; then
                echo "Succeed to create branch."
            else
                echo "Failed to create branch."
                exit 1
            fi
        fi
        cd ..
    done
}

## 选项列表
function display_help()
{
usage="$(basename "$0") [-h] [-c] [-p] [-r] [-t tag_name]
where:
    -h  display help
    -c  git clone
    -p  git pull
    -r  remove .git
    -b  branch product
    -o  checkout branch
    -t  tag product
    "
  echo "$usage"
  exit 0
}


## 入口函数
function main(){
    if [[ ! -n $* ]];then
        display_help
    fi
    get_product_list
    if [ -d ${SCRIPT_PATH}/remotes ];then
        cd "${SCRIPT_PATH}" && rm -rf ./remotes && mkdir ${SCRIPT_PATH}/remotes
    else
        mkdir ${SCRIPT_PATH}/remotes
    fi
    echo_cs "product list: ${PRODUCT_LIST}"
    while getopts ':hcprt:b:o:' option; do
        case "${option}" in
        h)
            display_help
            exit 0
        ;;
        c)
            git_clone_product
        ;;
        p)
            git_pull_product
        ;;
        r)
            remove_gitfile
        ;;
        o)
            #PRODUCT_PATH=${SCRIPT_PATH}/remotes
            #PRODUCT_LIST="fano-common-ucharts fano-portal-ucharts fano-speech-ucharts fano-callinter-ucharts fano-nlp-ucharts"
            #git_clone_product
            git_checkout $OPTARG
        ;;
        t)
            PRODUCT_PATH=${SCRIPT_PATH}/remotes
            PRODUCT_LIST="fano-common-ucharts fano-portal-ucharts fano-speech-ucharts fano-callinter-ucharts fano-nlp-ucharts"
            git_clone_product
            create_tag $OPTARG
        ;;
        b)
            PRODUCT_PATH=${SCRIPT_PATH}/remotes
            PRODUCT_LIST="fano-common-ucharts fano-portal-ucharts fano-speech-ucharts fano-callinter-ucharts fano-nlp-ucharts fano-focotalk-ucharts fano-prosearch-ucharts fano-navinter-ucharts fano-accobot-ucharts fano-data-pipeline-ucharts fano-demo-ucharts fano-bot-ucharts"
            git_clone_product
            create_branch $OPTARG
        ;;
        ?)
            display_help
            exit 1
        ;;
        esac
    done
}

main $*